package com.glorysoft.xztpda.ui.trackin

import android.os.Bundle
import android.os.Handler
import com.glorysoft.xztpda.entity.ui.ToastMessageBean
import com.glorysoft.xztpda.BR
import com.glorysoft.xztpda.R
import com.glorysoft.xztpda.base.BaseActivity
import com.glorysoft.xztpda.bindingadapter.CarrierCheckAdapter
import com.glorysoft.xztpda.databinding.ActivityTrackInEqpBinding
import com.glorysoft.xztpda.entity.LotInfoEntity
import kotlinx.android.synthetic.main.activity_track_in_eqp.*

/**
 * FileName: TrackInCarrier
 * Author: XJH
 * Date: 2022/4/12 17:04
 * Description:
 * History:
 */
class TrackInEqpActivity : BaseActivity<TrackInBaseViewModel,ActivityTrackInEqpBinding>() {

    private var mCarrierAdapter : CarrierCheckAdapter? = null


    override fun layoutId(): Int {
        return R.layout.activity_track_in_eqp
    }

    override fun initVariableId(): Int {
        return BR.trackInBaseViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle("开始作业")
        viewModel.lotInfoEntity = intent.getSerializableExtra("LOTINFO") as LotInfoEntity
        addActivity(this)
        setRightListener("提交"){
            val checkList = mCarrierAdapter!!.checkList
            for (i in 0 until checkList.size()) {
                if (!checkList[i]) {
                    viewModel.defUI.toastEvent.value = ToastMessageBean("当前有载具未校验",false)
                    return@setRightListener
                }
            }

            viewModel.trackIn("XZTSubEqpTrackIn-B")
        }
    }

    override fun initData() {
        mCarrierAdapter = CarrierCheckAdapter(null)
        rvCarrierList.adapter = mCarrierAdapter
//        scanCarrier()

        viewModel.defUI.startActivity.observe(this){
            finishAllActivity()
        }
    }

    /**
     * 载具验证
     */
    private fun scanCarrier() {
        initEdit(etCarrier,object : EditTextListener{
            override fun onTextChangeListener(s: String) {
                etCarrier.setText("")
                Handler().postDelayed({ etCarrier.requestFocus() }, 500)
                if (mCarrierAdapter?.data == null || mCarrierAdapter?.data!!.size <= 0) {
                    return
                }

                val data = mCarrierAdapter?.data
                for (i in data?.indices!!) {
                    if (data[i].equals(s, ignoreCase = true)) {
                        if (mCarrierAdapter?.checkList?.get(i)!!) {
                            viewModel.defUI.toastEvent.value = ToastMessageBean("当前载具已校验",false)
                        } else {
                            mCarrierAdapter?.setCheck(s)
                        }
                        return
                    }
                }
                viewModel.defUI.toastEvent.value = ToastMessageBean("当前载具不匹配",false)
            }

        })
    }
}